import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author yu
 * @date 2025年04月16日 19:01
 */
public class test3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        long[] weights = new long[n];
        for (int i = 0; i < n; i++) {
            weights[i] = scanner.nextInt();
        }

        List<List<Integer>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
        }

        for (int i = 0; i < n - 1; i++) {
            int u = scanner.nextInt();
            int v = scanner.nextInt();
            graph.get(u - 1).add(v - 1);
            graph.get(v - 1).add(u - 1);
        }

        int q = scanner.nextInt();
        for (int i = 0; i < q; i++) {
            int m = scanner.nextInt() - 1;
            long multiply = scanner.nextLong();
            dfsMultiply(graph, weights, m, multiply);
        }

        long[] results = new long[n];
        for (int i = 0; i < n; i++) {
            results[i] = 1;
            for (int neighbor : graph.get(i)) {
                results[i] *= weights[neighbor];
            }
            results[i] *= weights[i];
        }

        for (long result : results) {
            System.out.println(countTrailingZeros(result));
        }
    }

    private static void dfsMultiply(List<List<Integer>> graph, long[] weights, int root, long multiply) {
        weights[root] *= multiply;
        for (int neighbor : graph.get(root)) {
            dfsMultiply(graph, weights, neighbor, multiply);
        }
    }

    private static int countTrailingZeros(long num) {
        int count = 0;
        while (num % 10 == 0) {
            count++;
            num /= 10;
        }
        return count;
    }
}
